/**
 * 替换占位节点 为 目标节点
 * @param placeholderNode {Node} 占位节点 Node
 * @param transferNode {Node} 目标节点 Node
 * @param graph {Graph} Graph实例
 */
export function useReplaceNode({placeholderNode, transferNode, graph}) {
  // 定位
  const pNodePosition = placeholderNode.position()
  transferNode.position(pNodePosition.x, pNodePosition.y)

  // 边关系， 入边
  const pNodeIncomingEdges = graph.getIncomingEdges(placeholderNode) || []
  pNodeIncomingEdges.forEach(edge => {
    const target = edge.getTarget() // {port: string, cell: string}
    const targetCell = graph.getCellById(target.cell) //cell: CellMetaData 理论上等同于pNode
    const portGroup = targetCell.getPort(target.port).group //pNode group
    const eNodeGroupPorts = transferNode.getPortsByGroup(portGroup)
    eNodeGroupPorts.forEach(portMetaData => {
      edge.setTarget({
        cell: transferNode.id,
        port: portMetaData.id
      })
    })
  })
  // 边关系，出边
  const pNodeOutgoingEdges = graph.getOutgoingEdges(placeholderNode) || []
  pNodeOutgoingEdges.forEach(edge => {
    const source = edge.getSource() // {port: string, cell: string}
    const sourceCell = graph.getCellById(source.cell) //cell: CellMetaData 理论上等同于pNode
    const portGroup = sourceCell.getPort(source.port).group //pNode group
    const eNodeGroupPorts = transferNode.getPortsByGroup(portGroup)
    eNodeGroupPorts.forEach(portMetaData => {
      edge.setTarget({
        cell: transferNode.id,
        port: portMetaData.id
      })
    })
  })

  // 复制可能存在的数据
  transferNode.setData({
    placeholderNodeData: {
      ...placeholderNode.getData()
    }
  })

  graph.removeCell(placeholderNode)
}

/**
 * 将操作中的节点替换为PlaceholderNode
 * @param operatingNode
 * @param graph
 */
export function useReplaceNodeReverse({operatingNode, graph}) {
  debugger
  // 创建placeholderNode
  const placeholderNode = graph.addNode({
    shape: 'placeholder',
  })

  // 定位
  const opNodePosition = operatingNode.position()
  placeholderNode.position(opNodePosition.x, opNodePosition.y)

  // 边关系， 入边
  const opNodeIncomingEdges = graph.getIncomingEdges(operatingNode) || []
  opNodeIncomingEdges.forEach(edge => {
    const target = edge.getTarget() // {port: string, cell: string}
    const targetCell = graph.getCellById(target.cell) //cell: CellMetaData 理论上等同于pNode
    const portGroup = targetCell.getPort(target.port).group //pNode group
    const eNodeGroupPorts = placeholderNode.getPortsByGroup(portGroup)
    eNodeGroupPorts.forEach(portMetaData => {
      edge.setTarget({
        cell: placeholderNode.id,
        port: portMetaData.id
      })
    })
  })
  // 边关系，出边
  const opNodeOutgoingEdges = graph.getOutgoingEdges(operatingNode) || []
  opNodeOutgoingEdges.forEach(edge => {
    const source = edge.getSource() // {port: string, cell: string}
    const sourceCell = graph.getCellById(source.cell) //cell: CellMetaData 理论上等同于pNode
    const portGroup = sourceCell.getPort(source.port).group //pNode group
    const eNodeGroupPorts = placeholderNode.getPortsByGroup(portGroup)
    eNodeGroupPorts.forEach(portMetaData => {
      edge.setTarget({
        cell: placeholderNode.id,
        port: portMetaData.id
      })
    })
  })

  graph.removeCell(operatingNode)
}
